/*
 * SunSpotApplication.java
 *
 * Created on Apr 19, 2011 6:57:37 PM;
 */

package org.sunspotworld;

import com.sun.spot.core.resources.transducers.ISwitch;
import com.sun.spot.core.resources.transducers.ITriColorLED;
import com.sun.spot.core.resources.transducers.LEDColor;
import com.sun.spot.edemo.EDemoBoard;
import com.sun.spot.edemo.peripheral.EDemoSerial;
import com.sun.spot.espot.service.BootloaderListenerService;
import java.io.DataInputStream;
import java.io.IOException;

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
 * The startApp method of this class is called by the VM to start the
 * application.
 *
 * The manifest specifies this class as MIDlet-1, which means it will
 * be selected for execution.
 */
public class SunSpotApplication extends MIDlet {

    private EDemoSerial serial = new EDemoSerial();
    private DataInputStream in;

    private ITriColorLED[] led = EDemoBoard.getInstance().getLEDs();
    private ISwitch[] sw = EDemoBoard.getInstance().getSwitches();

    private byte[] buffer = new byte[3];
    private int data = 0;

    protected void startApp() throws MIDletStateChangeException {
        BootloaderListenerService.getInstance().start();   // monitor the USB (if connected) and recognize commands from host

        try {
            //Default USART configurations on the WiFly
            serial.setUSARTParams(9600, 8, "none", 1);
            in = serial.openDataInputStream();
            in.skipBytes(in.available());
        } catch(IOException e) {

        }

        //Set the color and make sure they are turned off
        for(int i = 0; i < led.length; i++) {
            led[i].setColor(LEDColor.GREEN);
            led[i].setOff();
        }

        //controlled loop to read in, parse, and display the number.
        while(sw[0].isOpen() || sw[1].isOpen()) {

            try {

                while(in.available() == 0) {
                    //Do nothing
                }

                if(in.available() > 3) {
                    in.skipBytes(in.available());
                    break;
                }

                in.readFully(buffer, 0, buffer.length);

                data += (Character.digit((char)buffer[0], 10)) * 100;
                //System.out.print(((Character.digit((char)buffer[0], 10)) * 100) + " + ");

                data += (Character.digit((char)buffer[1], 10)) * 10;
                //System.out.print(((Character.digit((char)buffer[1], 10)) * 10) + " + ");

                data += (Character.digit((char)buffer[2], 10));
                //System.out.print(((Character.digit((char)buffer[2], 10))) + " = ");

                System.out.println(data);

                for(int i = 0; i < 8; i++) {
                    if(((data >> i) & 1) == 1) {
                        led[7-i].setOn();
                    }
                    else {
                        led[7-i].setOff();
                    }
                }

                data = 0;

                in.skipBytes(in.available());


            } catch(IOException e){

            }
        }

        notifyDestroyed();                      // cause the MIDlet to exit
    }

    protected void pauseApp() {
        // This is not currently called by the Squawk VM
    }

    /**
     * Called if the MIDlet is terminated by the system.
     * It is not called if MIDlet.notifyDestroyed() was called.
     *
     * @param unconditional If true the MIDlet must cleanup and release all resources.
     */
    protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
    }
}
